package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.acticle.domain.*;
import cn.wolfcode.luowowo.acticle.query.DestinationQuery;
import cn.wolfcode.luowowo.acticle.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.acticle.query.TravelQuery;
import cn.wolfcode.luowowo.acticle.service.*;
import cn.wolfcode.luowowo.cache.prefix.DestinationStatisKeyPrefix;
import cn.wolfcode.luowowo.cache.service.IDestinationStatisRedisService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 前端目的地控制器
 */
@Controller
@RequestMapping("destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private ITravelService travelService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyContentService strategyContentService;

    @Reference
    private IDestinationStatisRedisService destinationStatisRedisService;

    @Reference
    private IDestinationThemeshitsService destinationThemeshitsService;

    @Reference
    private IDestinationTagService destinationTagService;


    @RequestMapping("")
    public String index(Model m, String name) {
        List<Region> hotRegions = regionService.getHotRegions();
        m.addAttribute("hotRegions", hotRegions);

        //themesHits
        m.addAttribute("themesHits", destinationThemeshitsService.list());

        //seasonsHits
        m.addAttribute("seasonsHits", destinationTagService.getSeason());

        m.addAttribute("name", name);

        return "destination/index";
    }


    @RequestMapping("guide")
    public String guide(Model m, Long id, @UserParam UserInfo userInfo) {
        List<Destination> toasts = destinationService.getToastById(id);
        Destination destination = toasts.remove(toasts.size() - 1);

        m.addAttribute("toasts", toasts);
        m.addAttribute("dest", destination);
        m.addAttribute("catalogs", strategyCatalogService.queryByDestId(id));

        m.addAttribute("strategyDetails", strategyDetailService.queryStrategyDetailByDestId(id));
     //查询该用户是否已经收藏
        boolean favor = destinationStatisRedisService.isFavor(DestinationStatisKeyPrefix.DESTINATION_STATIS_FAVOR, id, userInfo.getId());
        m.addAttribute("favor", favor?1:0);
        return "destination/guide";
    }

    @RequestMapping("getHotDestByRegionId")
    public String getHotDestByRegionId(Model m, Long regionId) {

        //1.查询数据
        //2.拼接Html

        //3.页面在拼接html块

        List<Destination> hotDestination = regionService.getHotDestination(regionId);

        int size = hotDestination.size();

        //leftDests  --  children
        List<Destination> left = hotDestination.subList(0, size / 2 + 1);
        List<Destination> right = hotDestination.subList(size / 2 + 1, size);
        m.addAttribute("leftDests", left);
        m.addAttribute("rightDests", right);
        m.addAttribute("regionId", regionId);

        return "destination/hotdestTpl";
    }

    @RequestMapping("/travels")
    public String travelsPage(Model model, @ModelAttribute("qo") TravelQuery qo) {


        model.addAttribute("pageInfo", travelService.query(qo));

        return "destination/travelTpl";
    }

    @RequestMapping("/surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo) {
        List<Destination> toasts = destinationService.getToastById(qo.getDestId());
        model.addAttribute("dest", toasts.remove(toasts.size() - 1));
        model.addAttribute("toasts", toasts);
        return "destination/survey";
    }


    @RequestMapping("/survey")
    public String survey(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo) {
        List<StrategyCatalog> catalogs = strategyCatalogService.queryByDestId(qo.getDestId());
        model.addAttribute("catalogs", catalogs);
        if (qo.getCatalogId() == -1L) {
            if (catalogs != null && catalogs.size() > 0)
                qo.setCatalogId(catalogs.get(0).getId());
        }
        StrategyCatalog catalog = strategyCatalogService.get(qo.getCatalogId());
        model.addAttribute("catalog", catalog);
        if (catalog != null && catalog.getDetails().size() > 0) {
            StrategyDetail detail = catalog.getDetails().get(0);
            detail.setStrategyContent(strategyContentService.get(detail.getId()));
            model.addAttribute("detail", detail);
        }
        return "destination/surveyTpl";
    }

    //getTagByHitId
    @RequestMapping("/getTagByHitId")
    public String getTagByHitId(Model m, Long hitId) {

        m.addAttribute("list", destinationTagService.getdestinationTagByDthId(hitId));
        m.addAttribute("hitId", hitId);
        return "destination/hitdestTpl";

    }

    //getDestByDestId
    @RequestMapping("/getDestByDestId")
    public String getDestByDestId(Model m, Long sitId) {

        DestinationTag DstTags = destinationTagService.getByPrimaryKey(sitId);
        String strDestIds = DstTags.getDest_id();
        String[] arrDestIdS = strDestIds.split(",");

        List<Destination> dests = destinationService.getDestByStrDestIds(arrDestIdS);
        List<Destination> dests1 = dests;


        List<Destination> list3 = dests.subList(0, 3);
        List<Destination> list4 = dests1.subList(3, 6);

        m.addAttribute("list3", list3);
        m.addAttribute("list4", list4);
        m.addAttribute("img", DstTags.getCoverUrl());
        m.addAttribute("id", DstTags.getId());

        m.addAttribute("sitId", sitId);
        return "destination/sHitdestTpl";

    }


    //获取标签,和内容的全部数据
    @RequestMapping("travelFilter")
    public String retrunRravelFilter(Model model, @ModelAttribute("qo") DestinationQuery qo, Long id, String tagId) {

        qo.setOrderBy("");

        model.addAttribute("id", id);

        //查询所有的DestinationTag
        List<DestinationTag> list = destinationTagService.list();


        //共享筛选的标签
        List<DestinationTag> month = new ArrayList<>();
        List<DestinationTag> festival = new ArrayList<>();
        List<DestinationTag> suitableForAllYear = new ArrayList<>();
        List<DestinationTag> season = new ArrayList<>();
        List<DestinationTag> travelMode = new ArrayList<>();
        List<DestinationTag> numberOfDays = new ArrayList<>();

        for (DestinationTag destinationTag : list) {
            if (destinationTag.getId() < 13) {
                //month
                month.add(destinationTag);
                model.addAttribute("month", month);
            }

            if (destinationTag.getId() > 12 && destinationTag.getId() < 22) {
                festival.add(destinationTag);
                //festival
                model.addAttribute("festival", festival);
            }

            // //suitableForAllYear
            if (destinationTag.getId() > 21 && destinationTag.getId() < 37) {
                suitableForAllYear.add(destinationTag);
                model.addAttribute("suitableForAllYear", suitableForAllYear);
            }

            //season
            if (destinationTag.getId() > 36 && destinationTag.getId() < 49) {
                season.add(destinationTag);
                model.addAttribute("season", season);
            }

            //travelMode
            if (destinationTag.getId() > 48 && destinationTag.getId() < 58) {
                travelMode.add(destinationTag);
                model.addAttribute("travelMode", travelMode);
            }

            //numberOfDays
            if (destinationTag.getId() > 57) {
                numberOfDays.add(destinationTag);
                model.addAttribute("numberOfDays", numberOfDays);
            }
        }



        List<Destination> destinations = new ArrayList<>();

        model.addAttribute("tagId", tagId);

        //如果id和 tagId的字符串都为空.直接那全部的目的地共享
        if (id == null && tagId == "") {
            model.addAttribute("pageInfo", destinationService.query(qo));

        } else if (id == null && tagId != "") {

            String[] arrTagId = tagId.split(",");

            if (arrTagId.length == 1) {

                Long longTagId = Long.valueOf(arrTagId[0]);
                DestinationTag dt = destinationTagService.getByPrimaryKey(longTagId);
                String dest_id = dt.getDest_id();

                if (dest_id == null) {
                    return "/destination/travelFilter";
                }
                //通过","切割然后转成Long 的目的地id
                String[] destIds = dest_id.split(",");
                Long[] ArrNum = (Long[]) ConvertUtils.convert(destIds, Long.class);
                qo.setDestIds(destIds);


            } else {

                List<String> ele = new ArrayList<>();

                Set<String> set = new HashSet<String>();
                List<String> strDestIds = destinationTagService.getDestIdByTagId(tagId);


                String[] arrDestIds = strDestIds.toArray(new String[strDestIds.size()]);

                for (String destId : arrDestIds) {
                    if (destId == null) {
                        return "/destination/travelFilter";
                    }

                    String[] arrdestIds = destId.split(",");
                    for (String arrdestId : arrdestIds) {
                        ele.add(arrdestId);

                    }
                }

                for (int a = 0; a < ele.size() - 1; a++) {
                    for (int j = a + 1; j < ele.size(); j++) {
                        if (ele.get(a).equals(ele.get(j))) {
                            set.add(ele.get(a));
                        }
                    }
                }
                List<String> arrdestIds = new ArrayList(set);
                String[] strdestIds = arrdestIds.toArray(new String[arrdestIds.size()]);
                if (strdestIds.length == 0) {
                    return "/destination/travelFilter";
                }
                qo.setDestIds(strdestIds);
            }

            model.addAttribute("pageInfo", destinationService.queryDest(qo));
        } else {


            //通过一个图片标签的id拿到,目的地的id(是个字符串)
            DestinationTag dt = destinationTagService.getByPrimaryKey(id);
            String dest_id = dt.getDest_id();

            if (dest_id == null) {
                return "/destination/travelFilter";
            }

            //通过","切割然后转成Long 的目的地id
            String[] destIds = dest_id.split(",");
            qo.setDestIds(destIds);
        }
        model.addAttribute("pageInfo", destinationService.queryDest(qo));
        return "/destination/travelFilter";
    }

    //收藏favor
    @RequestMapping("favor")
    @ResponseBody
    public Object favor(Long did, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            return new AjaxResult("请先登录", 102);
        }
        boolean ret = destinationStatisRedisService.favor(DestinationStatisKeyPrefix.DESTINATION_STATIS_FAVOR, did, userInfo.getId());
        return new AjaxResult(ret, "");
    }

}
